package cn.hpclub.server.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;

/**
 * 实体类 - 管理员
 * 
 */
@TableBind(tableName = "sms_code")
public class SmsCode extends Model<SmsCode> implements Serializable{

    private static final long   serialVersionUID = -57555379613217315L;

    public static final String  PHONE            = "phone";
    public static final String  CODE             = "code";
    public static final String  time             = "modify_time";
    public static final String COUNT = "count";
    public static final SmsCode dao              = new SmsCode();
    public static long          interval         = 1800000;

    public boolean updateCode(String phone, String code){
        if(Db.update("insert into " + Constant.Table.SMS_CODE + " value (?,?,now()) ON DUPLICATE KEY UPDATE " + time
                + "=now(),code=values(code)", new Object[] { phone, code }) > 0){
            return true;
        } else{
            return false;
        }
    }

    public void updateCodeLogin(String phone, String code){
        try{
            Db.update("insert into " + Constant.Table.SMS_CODE_LOGIN + " value (0,?,?,now())",
                      new Object[] { phone, code });
        } catch(Exception e){
            e.printStackTrace();
        }

    }

    public boolean verifyCode(String phone, String code){
        Record r = Db.findFirst("select * from " + Constant.Table.SMS_CODE + " where " + PHONE + "=" + phone + " and "
                + CODE + "=" + code);
        if(r == null){
            return false;
        }
        Date now = new Date();
        Date modify = r.getDate(time);
        long inte = now.getTime() - modify.getTime();

        if((now.getTime() - r.getDate(time).getTime()) > interval){
            return false;
        }
        return true;

    }

    public boolean verifyCodeLogin(String phone, String code){
        Record r = Db.findFirst("select * from " + Constant.Table.SMS_CODE_LOGIN + " where " + PHONE + "=" + phone
                + " and " + CODE + "=" + code);
        if(r == null){
            return false;
        }

        if(new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                .equals(new SimpleDateFormat("yyyy-MM-dd").format(r.getDate(time)))){
            return true;
        } else{
            return false;
        }
    }

    public void removeCode(String phone){
        Db.update("delete from " + Constant.Table.SMS_CODE + " where " + PHONE + "=" + phone);

    }

    public boolean countCode(String phone){
        try{
            Record r = Db.findFirst("select " + COUNT + " from " + Constant.Table.SMS_CODE_RECORD
                    + " where date_format(now(),'%Y-%m-%d')=date_format(" + time + ",'%Y-%m-%d') and " + PHONE + "=?",
                                    new Object[] { phone });
            if(r == null || r.getInt(COUNT) < 5){
                return true;
            } else{
                return false;
            }
        } catch(Exception e){

            e.printStackTrace();
            return true;
        }

    }

    public void updateCodeRecord(String phone){
        try{
            Db.update("insert into " + Constant.Table.SMS_CODE_RECORD
                    + " value (0,?,1,date_format(now(),'%Y-%m-%d')) ON DUPLICATE KEY UPDATE " + COUNT + "=" + COUNT
                    + "+1", new Object[] { phone });
        } catch(Exception e){
            e.printStackTrace();
        }

    }

}
